//
//  RegistViewController.m
//  YaoHeAPP
//
//  Created by 杨亚森 on 16/7/4.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "RegistViewController.h"
#import "Masonry.h"
#import "LoginVC.h"
#import "WebVC.h"
#import "Controller.h"
#import "Define.h"
#import "TongYong.h"
#import "QueryCompanyTableView.h"
#import "MJExtension.h"
#import "CompanyItemModel.h"
#import "RegistSuccessResultModel.h"
#import "RegistSuccess.h"
#import "RegistSuccessViewController.h"
#import "YunBaService.h"
@interface RegistViewController ()<UITextFieldDelegate,UIWebViewDelegate,UIAlertViewDelegate>
{
    UIButton  *selectBtn;
    int _timeNum;
    NSTimer *_timer;
    
}

@property(nonatomic,strong) QueryCompanyTableView *queryCompanyTableView; 
@property(nonatomic,strong) NSArray *companyItemArray;
@property(nonatomic,strong) UITextField  *companyText;
@property(nonatomic,strong) UITextField  *iphoneText;
@property(nonatomic,strong) UIButton  *yanzhengbutton;
@property(nonatomic,strong) UITextField  *yanzhengmaText;
@property(nonatomic,strong) UITextField  *setmimaText;
@property(nonatomic,strong) UITextField  *confirmText;
@property(nonatomic,strong) UITextField  *tuijianText;
@property(nonatomic,strong) NSString *companyString;
@property(nonatomic,strong) NSString *companyID;
@property(nonatomic,strong) RegistSuccessResultModel *registSuccessResultModel;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@end

@implementation RegistViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegistVC"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegistVC"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _companyItemArray=[NSArray array];
    
    self.view.backgroundColor = bgVCcolor;
    //加一个导航条
    UIView*navcBar = [[UIView alloc]init];
    navcBar.backgroundColor = [UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"快速注册";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(SCREEN_WIDTH/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(30);
        make.height.offset(28);
        make.bottom.offset(-6);
    }];
    
    //布局页面
    [self creatorView];

}

//创建页面
-(void)creatorView
{
    //公司名称
    UIView *companyView=[[UIView alloc]init];
    companyView.frame=CGRectMake(0, 70, SCREEN_WIDTH, 40);
    companyView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:companyView];
    
    //分割线
    UIView *companyLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    companyLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [companyView addSubview:companyLineView];
    
    //小星星图片
    UIImageView*companystartImage = [[UIImageView alloc]init];
    companystartImage.image = [UIImage imageNamed:@"bi_tian"];
    companystartImage.frame = CGRectMake(10, 15, 10, 10);
    [companyView addSubview:companystartImage];

    //提示标签
    UILabel*companyname = [[UILabel alloc]init];
    companyname.font = Font(14);
    companyname.text = @"公司名称:";
    companyname.frame = CGRectMake(companystartImage.right+10, 10, [self getTextwidthWithString:@"公司名称:"], 20);
    [companyView addSubview:companyname];
    //输入框
    _companyText=[[UITextField alloc]init];
    _companyText.font=Font(13);
    _companyText.frame=CGRectMake(companyname.right+5, 10, SCREEN_WIDTH-companyname.right-5-10, 20);
    _companyText.placeholder=@"请输入公司名称";
    _companyText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _companyText.borderStyle=UITextBorderStyleNone;
    _companyText.delegate=self;
    [_companyText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [_companyText addTarget:self action:@selector(EditingChanged) forControlEvents:UIControlEventEditingChanged];
    [companyView addSubview:_companyText];
    
    //手机号
    UIView *iphoneView=[[UIView alloc]init];
    iphoneView.frame=CGRectMake(0, companyView.bottom, SCREEN_WIDTH, 40);
    iphoneView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:iphoneView];
    
    //分割线
    UIView *iphoneLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    iphoneLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [iphoneView addSubview:iphoneLineView];
    
    //小星星图片
    UIImageView *iphonestartImage = [[UIImageView alloc]init];
    iphonestartImage.image = [UIImage imageNamed:@"bi_tian"];
    iphonestartImage.frame = CGRectMake(10, 15, 10, 10);
    [iphoneView addSubview:iphonestartImage];
    
    //提示标签
    UILabel*iphonename = [[UILabel alloc]init];
    iphonename.font = Font(14);
    iphonename.text = @"手机号:";
    iphonename.frame = CGRectMake(iphonestartImage.right+10, 10, [self getTextwidthWithString:@"手机号:"], 20);
    [iphoneView addSubview:iphonename];
    
    
    //获取验证码的按钮
    _yanzhengbutton=[UIButton buttonWithType:UIButtonTypeCustom];
    _yanzhengbutton.frame=CGRectMake(SCREEN_WIDTH-10-90,5, 90, 30);
    _yanzhengbutton.layer.borderWidth=1;
    _yanzhengbutton.titleLabel.font=[UIFont systemFontOfSize:13];
    _yanzhengbutton.layer.borderColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1].CGColor;
    [_yanzhengbutton setTitle:@"发送验证码" forState:UIControlStateNormal];
    [_yanzhengbutton setTitleColor:[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1] forState:UIControlStateNormal];
    [_yanzhengbutton addTarget:self action:@selector(sendValidation) forControlEvents:UIControlEventTouchUpInside];
    [iphoneView addSubview:_yanzhengbutton];

    //输入框
    _iphoneText=[[UITextField alloc]init];
    _iphoneText.font=Font(13);
    _iphoneText.frame=CGRectMake(iphonename.right+5, 10, SCREEN_WIDTH-iphonename.right-5-_yanzhengbutton.width-15, 20);
    _iphoneText.placeholder=@"请输入手机号";
    _iphoneText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _iphoneText.borderStyle=UITextBorderStyleNone;
    _iphoneText.delegate=self;
    [_iphoneText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [iphoneView addSubview:_iphoneText];
    
    //验证码
    UIView *yanzhengmaView=[[UIView alloc]init];
    yanzhengmaView.frame=CGRectMake(0, iphoneView.bottom, SCREEN_WIDTH, 40);
    yanzhengmaView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:yanzhengmaView];
    
    //分割线
    UIView *yanzhengmaLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    yanzhengmaLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [yanzhengmaView addSubview:yanzhengmaLineView];
    
    //小星星图片
    UIImageView *yanzhengmastartImage = [[UIImageView alloc]init];
    yanzhengmastartImage.image = [UIImage imageNamed:@"bi_tian"];
    yanzhengmastartImage.frame = CGRectMake(10, 15, 10, 10);
    [yanzhengmaView addSubview:yanzhengmastartImage];
    
    //提示标签
    UILabel*yanzhengmaname = [[UILabel alloc]init];
    yanzhengmaname.font = Font(14);
    yanzhengmaname.text = @"验证码:";
    yanzhengmaname.frame = CGRectMake(yanzhengmastartImage.right+10, 10, [self getTextwidthWithString:@"验证码:"], 20);
    [yanzhengmaView addSubview:yanzhengmaname];
    
    //输入框
    _yanzhengmaText=[[UITextField alloc]init];
    _yanzhengmaText.font=Font(13);
    _yanzhengmaText.frame=CGRectMake(yanzhengmaname.right+5, 10, SCREEN_WIDTH-yanzhengmaname.right-5-10, 20);
    _yanzhengmaText.placeholder=@"请输入验证码";
    _yanzhengmaText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _yanzhengmaText.borderStyle=UITextBorderStyleNone;
    _yanzhengmaText.delegate=self;
    [_yanzhengmaText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [yanzhengmaView addSubview:_yanzhengmaText];

    
    //设置密码
    UIView *setmimaView=[[UIView alloc]init];
    setmimaView.frame=CGRectMake(0, yanzhengmaView.bottom, SCREEN_WIDTH, 40);
    setmimaView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:setmimaView];
    
    //分割线
    UIView *setmimaLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    setmimaLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [setmimaView addSubview:setmimaLineView];
    
    //小星星图片
    UIImageView *setmimastartImage = [[UIImageView alloc]init];
    setmimastartImage.image = [UIImage imageNamed:@"bi_tian"];
    setmimastartImage.frame = CGRectMake(10, 15, 10, 10);
    [setmimaView addSubview:setmimastartImage];
    
    //提示标签
    UILabel*setmimaname = [[UILabel alloc]init];
    setmimaname.font = Font(14);
    setmimaname.text = @"设置密码:";
    setmimaname.frame = CGRectMake(setmimastartImage.right+10, 10, [self getTextwidthWithString:@"设置密码:"], 20);
    [setmimaView addSubview:setmimaname];
    
    //输入框
    _setmimaText=[[UITextField alloc]init];
    _setmimaText.font=Font(13);
    _setmimaText.frame=CGRectMake(setmimaname.right+5, 10, SCREEN_WIDTH-setmimaname.right-5-10, 20);
    _setmimaText.placeholder=@"请设置密码";
    _setmimaText.secureTextEntry = YES;
    _setmimaText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _setmimaText.borderStyle=UITextBorderStyleNone;
    _setmimaText.delegate=self;
    [_setmimaText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [setmimaView addSubview:_setmimaText];

    
    
    //确认密码
    UIView *confirmView=[[UIView alloc]init];
    confirmView.frame=CGRectMake(0, setmimaView.bottom, SCREEN_WIDTH, 40);
    confirmView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:confirmView];
    
    //分割线
    UIView *confirmLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    confirmLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [confirmView addSubview:confirmLineView];
    
    //小星星图片
    UIImageView *confirmstartImage = [[UIImageView alloc]init];
    confirmstartImage.image = [UIImage imageNamed:@"bi_tian"];
    confirmstartImage.frame = CGRectMake(10, 15, 10, 10);
    [confirmView addSubview:confirmstartImage];
    
    //提示标签
    UILabel*confirmname = [[UILabel alloc]init];
    confirmname.font = Font(14);
    confirmname.text = @"确认密码:";
    confirmname.frame = CGRectMake(confirmstartImage.right+10, 10, [self getTextwidthWithString:@"确认密码:"], 20);
    [confirmView addSubview:confirmname];
    
    //输入框
    _confirmText=[[UITextField alloc]init];
    _confirmText.font=Font(13);
    _confirmText.frame=CGRectMake(confirmname.right+5, 10, SCREEN_WIDTH-confirmname.right-5-10, 20);
    _confirmText.placeholder=@"请确认密码";
    _confirmText.secureTextEntry = YES;
    _confirmText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _confirmText.borderStyle=UITextBorderStyleNone;
    _confirmText.delegate=self;
    [_confirmText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [confirmView addSubview:_confirmText];

    
    
    
    //推荐人手机号
    UIView *tuijianView=[[UIView alloc]init];
    tuijianView.frame=CGRectMake(0, confirmView.bottom, SCREEN_WIDTH, 40);
    tuijianView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:tuijianView];
    
    //分割线
    UIView *tuijianLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, SCREEN_WIDTH, 0.5)];
    tuijianLineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [tuijianView addSubview:tuijianLineView];
    
    
    //提示标签
    UILabel*tuijianname = [[UILabel alloc]init];
    tuijianname.font = Font(14);
    tuijianname.text = @"推荐人手机号:";
    tuijianname.frame = CGRectMake(25, 10, [self getTextwidthWithString:@"推荐人手机号:"], 20);
    [tuijianView addSubview:tuijianname];
    
    //输入框
    _tuijianText=[[UITextField alloc]init];
    _tuijianText.font=Font(13);
    _tuijianText.frame=CGRectMake(tuijianname.right+5, 10, SCREEN_WIDTH-tuijianname.right-5-10, 20);
    _tuijianText.placeholder=@"选填项";
    _tuijianText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _tuijianText.borderStyle=UITextBorderStyleNone;
    _tuijianText.delegate=self;
    [_tuijianText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [tuijianView addSubview:_tuijianText];

    
    
    //同意协议选择框
    selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [selectBtn setBackgroundImage:[UIImage imageNamed:@"okandborder"] forState:UIControlStateNormal];
    selectBtn.selected=YES;
    [selectBtn addTarget:self action:@selector(selectDelegate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(320);
        make.left.offset(12);
        make.height.offset(15);
        make.width.offset(15);
    }];
    
    //我已阅读并同意协议的label
    UILabel*delegateLab=[[UILabel alloc]init];
    //    delegateLab.backgroundColor=[UIColor yellowColor];
    delegateLab.font=[UIFont systemFontOfSize:12];
    delegateLab.text=@"我已阅读并同意协议";
    delegateLab.textColor=[UIColor grayColor];
    [self.view addSubview:delegateLab];
    [delegateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(320);
        make.left.equalTo(selectBtn.mas_right).offset(3);
        make.height.offset(15);
        make.width.offset(110);
    }];
    
    //吆喝协议
    UIButton*xieyi=[UIButton buttonWithType:UIButtonTypeCustom];
    xieyi.titleLabel.font=[UIFont systemFontOfSize:13];
    [xieyi setTitle:@"《吆喝协议》" forState:UIControlStateNormal];
    [xieyi setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [xieyi addTarget:self action:@selector(yaoheDeledate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xieyi];
    [xieyi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(320);
        make.left.equalTo(delegateLab.mas_right).offset(1);
        make.height.offset(15);
        make.width.offset(78);
    }];
    
    //立即注册按钮
    UIButton*registBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [registBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [registBtn addTarget:self action:@selector(registBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registBtn];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(370);
        make.left.offset(50);
        make.right.offset(-50);
        make.height.offset(40);
    }];

}

//立即注册
-(void)registBtn
{
    if (selectBtn.selected==NO)
    {
        UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"亲，你需要同意我们的协议才能进行注册，谢谢合作。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    
    if (![TongYong isBlankObjectAndStr:_companyText.text]) {

        [dic setValue:_companyText.text forKey:@"MEMBER_NAME"];//个人注册时 选择或输入的公司名称
        
        NSString *companyST=_companyText.text;
        
        for (int i=0; i<_companyItemArray.count;i++) {
            
            CompanyItemModel *temp_companyItemModel=_companyItemArray[i];
            
            if ([companyST isEqualToString:temp_companyItemModel.COMPANY_NAME]) {
                _companyID=temp_companyItemModel.COMPANY_ID;
            }
        }
        
        if (![TongYong isBlankObjectAndStr:_companyID]) {
            
            [dic setValue:_companyID forKey:@"MEMBER_ID"];//如果是通过智能感知选择的公司，此字段必填 ；如果是自己输入的尚未注册的公司，则不用传值。
        }
        
    }else {
        ALERTSHOW(@"公司名称不能为空");
        return;
    }
    
    //判断用户手机输入的内容
    if (_iphoneText.text.length!=11)
    {
        ALERTSHOW(@"手机号格式不正确");
        return;
    }else{
        [dic setValue:_iphoneText.text forKey:@"MOBILE"];//注册手机号
    }
    if (![TongYong isBlankObjectAndStr:_yanzhengmaText.text]){
    
       [dic setValue:_yanzhengmaText.text forKey:@"CHECK_CODE"];//验证码
        
    }else{
        ALERTSHOW(@"请输入验证码");
        return;
    }
    
    //判断密码
    if (_setmimaText.text.length<6||_setmimaText.text.length>20)
    {
        ALERTSHOW(@"密码格式不正确，请重新输入");
        return;
    }else{
        
        //判断确认密码
        if (![_setmimaText.text isEqualToString:_confirmText.text])
        {
            ALERTSHOW(@"两次输入的密码不一致，请重新输入");
            return;
        }else{
        
             [dic setValue:_setmimaText.text forKey:@"PASSWORD"];//密码
             [dic setValue:_confirmText.text forKey:@"CHECK_PASSWORD"];//确认密码
        
        }
    
    }
    _dataDic=[[NSMutableDictionary alloc]init];
    self.dataDic=dic;
    [self companyIsLock:_companyText.text];
 
}

//发送验证码
-(void)sendValidation
{
    NSDictionary*dic=@{@"MOBILE":_iphoneText.text};
    [Controller requestWithURL:PostSendRegisterCheckCode jsondic:dic finishBlock:^(ClassData *clasData) {
        
        if ([clasData.number integerValue]==0) {
            _timeNum=[NSString stringWithFormat:@"%@",clasData.data].intValue;
            //将其注销重新开始
            if (_timer.isValid) {
                [_timer invalidate];
            }
            _timer=nil;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:@"" repeats:YES];
        }
        else{
            ALERTSHOW(clasData.message);
        }
    }];
}

-(void)refreshLessTime
{
    _timeNum=_timeNum-1;
    if (_timeNum==0) {
        //将其注销重新开始
        if (_timer.isValid) {
            [_timer invalidate];
        }
        _timer=nil;
        [_yanzhengbutton setTitle:@"重新发送" forState:UIControlStateNormal];
        [_yanzhengbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _yanzhengbutton.layer.borderColor=[UIColor redColor].CGColor;
        
    }else{
        NSString *sting=[NSString   stringWithFormat:@"剩余 %ds",_timeNum];
        [_yanzhengbutton setTitle:sting forState:UIControlStateNormal];
        [_yanzhengbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _yanzhengbutton.layer.borderColor=[UIColor grayColor].CGColor;
    }
 
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}
//吆喝协议按钮
-(void)yaoheDeledate
{
    WebVC*webVC=[WebVC new];
    webVC.titleTxt=@"吆喝协议";
    webVC.urlString=[NSString stringWithFormat:@"%@/WEB/mobile/register_agreement.aspx",BESTHOST];
    [self.navigationController pushViewController:webVC animated:YES];
}
//打勾框框、同意协议框
-(void)selectDelegate
{
    if (selectBtn.selected==YES)
    {
        [selectBtn setBackgroundImage:[UIImage imageNamed:@"Agreed"] forState:UIControlStateNormal];
        selectBtn.selected=NO;
        return;
    }
    if (selectBtn.selected==NO) {
        [selectBtn setBackgroundImage:[UIImage imageNamed:@"okandborder"] forState:UIControlStateNormal];
        selectBtn.selected=YES;
        return;
    }
    
}
//计算文字宽度（设置label的高度）
-(NSInteger)getTextwidthWithString:(NSString *)string
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT,38) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    return rect.size.width+8;
}

//返回登录界面
-(void)backLogin
{
    [self.navigationController popViewControllerAnimated:YES];
}

//键盘弹下
-(void)keyboardDissmiss
{
    return;
}

//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIView *suview=textField.superview;
    CGRect frame = suview.frame;
    int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0){
    
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
    }
    [UIView commitAnimations];
    
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

//改变输入框的内容的时候调用的方法
-(void)EditingChanged
{
    if (![TongYong isBlankObjectAndStr:_companyText.text]) {
      //调取查找相关公司的名称 //进行查询
      [self queryCopmany];
    }else{
     [_queryCompanyTableView removeFromSuperview];
    }
}

//公司名称模糊查询
-(void)queryCopmany
{
    NSDictionary*dic=@{@"COMPANY_NAME":_companyText.text};
    [Controller requestWithURL:PostlikelyCompanyName jsondic:dic finishBlock:^(ClassData *classData) {
        
        if ([classData.number integerValue]==0) {
           
            _companyItemArray = [CompanyItemModel mj_objectArrayWithKeyValuesArray:classData.data];
            
            if (_companyItemArray.count==0) {
               
                [_queryCompanyTableView removeFromSuperview];
                
            }else{
            
                [self.view addSubview:self.queryCompanyTableView];
                self.queryCompanyTableView.QueryCompanyList =_companyItemArray;
                [self.queryCompanyTableView reloadData];
            
            }
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];


}

- (QueryCompanyTableView *)queryCompanyTableView {
    
    if (!_queryCompanyTableView) {
        _queryCompanyTableView = [[QueryCompanyTableView alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 350) style:UITableViewStylePlain ];
        __block RegistViewController *weakSelf = self;
        _queryCompanyTableView.cancelActionBlock = ^() {
            [weakSelf closeIt];
        };
        
        _queryCompanyTableView.didSelectRowBlock = ^(NSInteger indexPathRow) {
            CompanyItemModel *companyItemModel = weakSelf.companyItemArray[indexPathRow];
            weakSelf.companyText.text=companyItemModel.COMPANY_NAME;
            [weakSelf closeIt];

        };
    }
    return _queryCompanyTableView;
}
//判断是否被锁
-(void)companyIsLock:(NSString *)COMPANY_NAME{
    
    NSDictionary*dic=@{@"MEMBER_NAME":COMPANY_NAME};
    [Controller requestWithURL:PostQueryCompanyInfoByName jsondic:dic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            
            NSString *st=[NSString stringWithFormat:@"%@",classData.data[@"COMPANY_LOCK"]];
            if ([st isEqualToString:@"1"]) {
                //被锁
                UIAlertView*alertViewCT=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"贵公司已被锁定，如有问题请拨打客服热线400-885-0051" delegate:self cancelButtonTitle:@"再想想" otherButtonTitles:@"立即注册",@"电话咨询",nil];
                alertViewCT.tag=20;
                [alertViewCT show];

            }else{
                
                //立即注册
                [self lijizhuce];
            
            }
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];

    
}
//警告框的协议方法 下方两个按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==20) {
        if (buttonIndex==0) {
            
        }else if (buttonIndex==1){
            //立即注册
            [self lijizhuce];
            
        }else if (buttonIndex==2){
            //电话咨询
            //进入打电话页面
            UIWebView *webview = [[UIWebView alloc] init];
            NSString *urlst=[NSString stringWithFormat:@"tel:4008850051"];
            [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlst]]];
            [self.view addSubview:webview];
        }
    }
}
//立即注册
-(void)lijizhuce{
    //立即注册
    [Controller requestWithURL:PostRegister jsondic:self.dataDic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            NSString *userid=[NSString stringWithFormat:@"%@",classData.data[@"USER_ID"]];
            NSString *publisher=[NSString stringWithFormat:@"%@",classData.data[@"SESSION_ID"]];
            [[NSUserDefaults standardUserDefaults] setObject:userid forKey:USERID];
            [[NSUserDefaults standardUserDefaults] setObject:publisher forKey:PUBLISHER];
            [YunBaService subscribe:userid resultBlock:^(BOOL succ, NSError *error){
                if (succ) {
                } else {
                }
            }];
            [[NSUserDefaults standardUserDefaults]setObject:@"成功" forKey:@"状态"];
            _registSuccessResultModel = [RegistSuccessResultModel mj_objectWithKeyValues:classData.data];
            ALERTSHOW(_registSuccessResultModel.USER_SCORE_INC_MSG);
            RegistSuccessViewController *registsuVC = [[RegistSuccessViewController alloc] init];
            registsuVC.RegistpeoplePhone=_registSuccessResultModel.REGMOBILE;
            registsuVC.MasterPhone=_registSuccessResultModel.MAIN_MOBILE;
            registsuVC.IS_APPLY=_registSuccessResultModel.IS_APPLY;
            [self.navigationController pushViewController:registsuVC animated:YES];
             NSString *regtype=[NSString stringWithFormat:@"%@",_registSuccessResultModel.REGTYPE];
            [[NSUserDefaults standardUserDefaults] setObject:regtype forKey:ISREGTYPE];
            
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];
    
}

-(void)closeIt
{
    [_queryCompanyTableView removeFromSuperview];

}

@end
